<template>
  <el-row>
    <!-- 子组件使用父组件传来的数据 -->
    <el-col :span="20">{{ treenode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treenode.manager }}</el-col>
        <el-col :span="12">
          <!-- 下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <!-- isheader默认为false,为true时显示 -->
              <el-dropdown-item v-if="!isheader" command="edit"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isheader" command="del"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDpt } from '../../../api/department'
export default {
  name: 'TreeTool',
  //   子组件接收父组件传来的数据
  props: {
    treenode: {
      type: Object,
      require: true
    },
    isheader: {
      type: Boolean,
      default: false
    }
  },
  created () {
    // console.log(this.treenode)
  },
  methods: {
    handleCommand (cmd) {
      console.log(cmd)
      switch (cmd) {
        case 'add':
          // 新增
          // 点击新增，发消息给父组件
          this.$emit('doadd', this.treenode.id)
          break
        case 'edit':
          // 点击编辑，发消息给父组件
          this.$emit('doadd', this.treenode.id)
          break
        case 'del':
          break
      }
      if (cmd === 'del') {
        // 弹出确认窗
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            // 发起删除请求
            await delDpt(this.treenode.id)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            // 重新获取新的部门列表
            this.$emit('getnewdpm')
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    }
  }
}
</script>

<style></style>
